/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * under the License.
 */
package shopping.front.onthefly;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseOnTheFlyAction;
import shopping.model.ProductAnswer;
import shopping.model.ProductQuestion;

import java.util.List;

/**
 * Copyright 2010 mazhao.
 * <p/>
 * User: mazhao
 * Date: 2010/11/21
 * Time: 15:09:59
 */
@org.apache.struts2.convention.annotation.Namespace("/front/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value = "shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
        @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
        @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class ProductQuestionAction extends BaseOnTheFlyAction {
    // ===============================
    // for add new question
    // ===============================
    private int productId;
    private String question;
    private String username;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Action(value = "/front/onthefly/addquestion",
            results = {
                    @Result(name = "success", type = "json", params = {"ignoreHierarchy", "false"})
            }
    )
    public String add() {
        if (log.isDebugEnabled()) {
            log.debug("enter front/onthefly/addquestion action");
            log.debug("productId:" + productId);
            log.debug("question: " + question);
            log.debug("username: " + username);
        }
        ProductQuestion pq = new ProductQuestion();
        pq.setProductId(productId);
        pq.setQuestion(question);
        pq.setUsername(username);
        try {
            pq.save();
        } catch (Exception ex) {
            ex.printStackTrace();
            this.setOnTheFlyStatus(false);
            this.setOnTheFlyMessage("error while saving question");
        }
        this.setOnTheFlyStatus(true);
        this.setOnTheFlyMessage("add question success");

        log.debug("question add successfully");
        return SUCCESS;
    }

    // ===============================
    // for list all question
    // ===============================

    private List<ProductQuestion> questions;

    public List<ProductQuestion> getQuestions() {
        return questions;
    }

    public void setQuestions(List<ProductQuestion> questions) {
        this.questions = questions;
    }

    @Action(value = "/front/onthefly/listquestion",
            results = {
                    @Result(name = "success", type = "json", params = {"ignoreHierarchy", "false"})
            }
    )
    public String list() {
        if(log.isDebugEnabled()) {
            log.debug("enter front/onthefly/listquestion action");
            log.debug("product id:" + productId);
        }
        this.questions = ProductQuestion.getQuestion4Product(productId);

        if(log.isDebugEnabled()) {
            for(ProductQuestion pq : questions) {
                log.debug(pq);
            }
        }
        return SUCCESS;
    }

    // ==================================
    // for answer
    // ==================================
    private int questionId;
    private String answer;

    public int getQuestionId() {
        return questionId;
    }

    public void setQuestionId(int questionId) {
        this.questionId = questionId;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    @Action(value = "/front/onthefly/answerquestion",
            results = {
                    @Result(name = "success", type = "json", params = {"ignoreHierarchy", "false"})
            }
    )
    public String answer() {
        if(log.isDebugEnabled()) {
            log.debug("enter front/onthefly/answerquestion action");
            log.debug("question id " + questionId);
            log.debug("answer " + answer);
            log.debug("user name " + username);
        }

        ProductAnswer pa = new ProductAnswer();
        pa.setQuestionId(questionId);
        pa.setAnswer(answer);
        pa.setUsername(username);
        try{
            pa.save();
        }catch(Exception ex) {
            ex.printStackTrace();
            this.setOnTheFlyMessage("failed to answer the qeustion " + questionId);
            this.setOnTheFlyStatus(false);
        }

        this.setOnTheFlyMessage("success to answer the qeustion " + questionId);
        this.setOnTheFlyStatus(true);

        return SUCCESS;
    }
}
